/**
 * Created by George on 2018/3/18.
 */
define(['angular', 'util'], function (angular, util) {
    "use strict";
    // 将本控制器函数作为结果返回给router.js
    var noteCtrl = function ($scope, $http) {
        $scope.welcome_info = "笔记管理";
        $scope.noteListDatas = [];
        $scope.emptyHtml = "<div>内容为空</div>";


        $scope.deleteNote = function (data) {
            var msg = "您真的确定要删除吗？";
            if (!confirm(msg)) {
                return;
            }
            console.log(data);
            $http.delete('/basketballfire/rest/note?id=' + data.id, data).then(function (req) {
                console.log("success!");
                alert("删除成功！");
                initOrUpdateData();
            }, function (req) {
                console.log("failed!");
                alert("删除成功！");
            });
        };

        function init() {
            $scope.noteListDatas = [];
            initOrUpdateData();
        }

        function initOrUpdateData() {
            $http.get("/basketballfire/rest/note/list").then(function (data, status) {
                console.log(data);
                var notesData = data.data.data;
                $scope.noteListDatas = [];
                for (var i = 0; i < notesData.length; i++) {
                    var tmpNote = notesData[i];
                    var note = {
                        id: tmpNote.id,
                        name: tmpNote.name,
                        content: tmpNote.content || $scope.emptyHtml,
                        date: tmpNote.date,
                        createTime: getFormatDate(tmpNote.createTime),
                        lastUpdateTime: getFormatDate(tmpNote.lastUpdateTime)
                    };
                    $scope.noteListDatas.push(note);
                }

            });
        }

        function getFormatDate(time) {
            return new Date(time).format("yyyy-MM-dd HH:mm:ss");
        }

        //app start
        init();
    };

    return noteCtrl;
});